import router from "@/router";
import {getToken} from "@/utils/auth";
import {isLogin} from "@/utils/user";
import Cookies from "js-cookie";
import Vue from "vue";

const whiteList = ['/user/login', '/404', '/user/law', '/user/privacy', '/home/index', '/news/index', '/skill/detail', '/difficult-job/difficult-job', '/difficult-job/difficult-detail',
'/company/company','/job-fair/job-fair'];
router.beforeEach(async (to, from, next) => {
    const login = isLogin();
    const hasToken = getToken()
    if (to.path === '/') {
        next({path: '/home/index'})
    } else if (['/mine-center/candidate','/mine-center/enterprise','/position/position-detail','/job-careers/job-careers','/skill/skill-index',
        '/policyIndex','/policyList','/skillNewDetails','/live/live','/job-fair/index','/position/position-filtrate','/personal/personal-filtrate','/skill/course-list',
    '/skill/course-list','/skill/video-list','/skill/policy-list'].includes(to.path)) {
        next();
    } else {
        if (hasToken) {
            if (['/enterprise/authorization/person-info', '/enterprise/authorization/authentication',
                '/enterprise/authorization/business', '/enterprise/authorization/examine'].includes(to.path)) {
                next();
                return
            }
            const identity = Cookies.get('identity');
            const user = Cookies.get('user') ? JSON.parse(Cookies.get('user')) : null;
            console.log(user)
            if (user) {
                if (identity == 1) {
                    if (user.infoStatus == 0) {//如果没有完善个人信息就强制跳转到个人中心
                        Vue.prototype.$message.error('请先完善个人信息');
                        next({path: '/mine-center/candidate'})
                        return;
                    }
                    if (user.resumeStatus == 0) {
                        Vue.prototype.$message.warning('简历信息未完善！');
                    }
                    next();
                } else {
                    if(user.authenticationStatus == 9&&user.comStatus == 2)
                    {
                        Vue.prototype.$message.error('请先完善企业信息');
                        next({path: '/mine-center/enterprise?tableId=6'});
                        return;
                    }
                    switch (user.authenticationStatus) {
                        case 0:
                            next({path: '/enterprise/authorization/person-info'});
                            return;
                        case 1:
                            next({path: '/enterprise/authorization/authentication'});
                            return;
                        case 2:
                            next({path: '/enterprise/authorization/business'});
                            return;

                    }

                    next();
                }
            } else {
                Cookies.remove('user');
                Cookies.remove('identity');
                Cookies.remove('web_login_auth');
                Vue.prototype.$message({
                    type: 'error',
                    message: '出错，请重新登录',
                    onClose: () => {
                        next({path: '/user/login'});
                    }
                });
            }
        } else {
            if (whiteList.indexOf(to.path) !== -1) {
                next()
            } else {
                next(`/user/login`)
            }
        }
    }
})
